/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.mongoservice;

import cn.ac.iie.di.wx.vo.RealIDQueryVo;
import java.util.HashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class RealIdMongoThread implements Runnable {

    public MongoHelper helper;
    public LinkedBlockingQueue<RealIDQueryVo> realIdBlockingQueue;
    public static Logger logger = Logger.getLogger(RealIdMongoThread.class.getName());

    public RealIdMongoThread(MongoHelper helper, LinkedBlockingQueue<RealIDQueryVo> blockingQueue) {
        this.helper = helper;
        this.realIdBlockingQueue = blockingQueue;
    }

    public MongoHelper getHelper() {
        return helper;
    }

    public void setHelper(MongoHelper helper) {
        this.helper = helper;
    }

    public LinkedBlockingQueue<RealIDQueryVo> getBlockingQueue() {
        return realIdBlockingQueue;
    }

    public void setBlockingQueue(LinkedBlockingQueue<RealIDQueryVo> blockingQueue) {
        this.realIdBlockingQueue = blockingQueue;
    }

    @Override
    public void run() {
        while (true) {
            try {
                HashMap<String, Object> hashMap = new HashMap<String, Object>();
                for (int i = 0; i < 10; i++) {
                    RealIDQueryVo vo = realIdBlockingQueue.poll(1000, TimeUnit.SECONDS);
                    if (vo != null) {
                        hashMap.put(vo.getId(), vo.getUid());
                    }
                }
                if (hashMap.size() > 0) {
                    helper.update("_id", "uid", hashMap);
                    logger.info("update realId to wxuser mongoDB");
                }

            } catch (InterruptedException ex) {
                logger.error(ex.getMessage(), ex);
            }
        }
    }

}
